package com.elong.businesstravel.modules.home.adapter;

import java.util.List;

import com.android.support.jhf.iphonetreeview.IphoneTreeView.PinnedHeaderAdapter;
import com.elong.businesstravel.ELongBusinessTravelApplication;
import com.elong.businesstravel.R;
import com.elong.businesstravel.entitys.CityPoi;
import com.elong.businesstravel.entitys.Poi;
import com.elong.businesstravel.modules.home.HotelLocationSelectActivity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class HotelLocationSelectAdapter extends BaseExpandableListAdapter
		implements PinnedHeaderAdapter {

	public static final String CURRENT_POSITION = "当前位置";
	public static final String NO_LIMIT = "不限";

	private Context mContext;
	private ELongBusinessTravelApplication mApplication;
	private List<CityPoi> mCityPois;

	public HotelLocationSelectAdapter(Context context, List<CityPoi> cityPois) {
		mContext = context;
		mCityPois = cityPois;

		mApplication = (ELongBusinessTravelApplication) context
				.getApplicationContext();

		CityPoi cityPoi = new CityPoi();
		cityPoi.name = HotelLocationSelectAdapter.CURRENT_POSITION;
		Poi poi = new Poi();
		poi.name = HotelLocationSelectAdapter.CURRENT_POSITION;
		cityPoi.pois.add(poi);

		CityPoi cityPoi1 = new CityPoi();
		cityPoi1.name = HotelLocationSelectAdapter.NO_LIMIT;
		Poi poi1 = new Poi();
		poi1.name = HotelLocationSelectAdapter.NO_LIMIT;
		cityPoi1.pois.add(poi1);

		mCityPois.add(0, cityPoi1);
		if (null != mApplication.mBdLocation)
			mCityPois.add(0, cityPoi);
	}

	@Override
	public int getGroupCount() {
		return mCityPois.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return mCityPois.get(groupPosition).pois.size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return mCityPois.get(groupPosition).name;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		String nameString = mCityPois.get(groupPosition).pois
				.get(childPosition).name;
		if ("".equals(nameString)) {
			nameString = mCityPois.get(groupPosition).pois.get(childPosition).bussinename;
		}
		if ("".equals(nameString)) {
			nameString = mCityPois.get(groupPosition).pois.get(childPosition).districtsname;
		}
		return mCityPois.get(groupPosition).pois.get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		if (null == convertView) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.group_list_text, null);
		}
		TextView groupTextView = (TextView) convertView
				.findViewById(R.id.groupTextView);
		groupTextView.setText(mCityPois.get(groupPosition).name);
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		if (null == convertView) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.child_list_text, null);
		}
		TextView childTextView = (TextView) convertView
				.findViewById(R.id.childTextView);
		String nameString = mCityPois.get(groupPosition).pois
				.get(childPosition).name;
		if ("".equals(nameString)) {
			nameString = mCityPois.get(groupPosition).pois.get(childPosition).bussinename;
		}
		if ("".equals(nameString)) {
			nameString = mCityPois.get(groupPosition).pois.get(childPosition).districtsname;
		}
		childTextView.setText(nameString);
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	@Override
	public int getPinnedHeaderState(int groupPosition, int childPosition) {
		return ALL_GROUP_EXPAND_NO_CLICK;
		// return PINNED_HEADER_GONE;
	}

	@Override
	public void configurePinnedHeader(View header, int groupPosition,
			int childPosition, int alpha) {
		TextView groupTextView = (TextView) header
				.findViewById(R.id.groupTextView);
		groupTextView.setText(mCityPois.get(groupPosition).name);
	}

	@Override
	public boolean getChildLastView(int childPosition) {
		return false;
	}

	@Override
	public List<String> getIndexList() {
		return null;
	}

}
